908B - New Year and Buggy Bot - CodeForces Solution


brute force implementation *1200

Please click on ads to support us..

Python Code:

import itertools

n, m = map(int, input().rstrip().split())
mas = []
start = [0, 0]
end = [0, 0]
for i in range(n):
    s = input()
    for j in range(len(s)):
        if s[j] == 'S':
            start = [i, j]
        elif s[j] == 'E':
            end = [i, j]
    mas.append(s)
k = input()

cou = 0
for i1, i2, i3, i4 in itertools.permutations(([0, 1], [1, 0], [-1, 0],  [0, -1])):
    x, y = start
    for i in k:
        if i == '0':
            x += i1[0]
            y += i1[1]
        elif i == '1':
            x += i2[0]
            y += i2[1]
        elif i == '2':
            x += i3[0]
            y += i3[1]
        elif i == '3':
            x += i4[0]
            y += i4[1]
        if x >= n or x < 0 or y >= m or y < 0 or mas[x][y] == '#':
            break
        elif [x, y] == end:
            cou += 1
            break

print(cou)

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>

#define ll long long
#define ld long double
#define el '\n'
#define pi acos(-1)
#define IO  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sumn(n) n*(n+1)/2
using namespace std;
template<typename T>
#pragma warning(suppress : 4996)
void print(T t, string s = " ") {
    for (auto e : t) {
        cout << e << s;
    }
    //cout << endl;
}
template<typename T>
void print(queue<T> q, string s = " ") {
    while (q.size()) {
        cout << q.front() << s;
        q.pop();
    }
    //cout << endl;
}
template<typename T>
void print(stack<T> st, string s = " ") {
    while (st.size()) {
        cout << st.top() << s;
        st.pop();
    }
    //cout << endl;
}
ll min();
ll max();
const int N = 1e7 + 1, mod = 1e9 + 7, inf = 1e9, pp = 31, K = 20;
int countBits(ll x) {
    int cntr = 0;
    while (x) {
        x >>= 1;
        cntr++;
    }
    return cntr;
}
ll sumRange(ll a, ll b, bool inclusive = true) {
    if (a == b)return 0;
    ll gg = sumn(b) - sumn(a) + a;
    if (!inclusive) {
        gg = gg - a - b;
    }
    return gg;
}
ll countSetBits(ll n)
{
    ll count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
int countdigits(ll x) {
    int cntr = 0;
    while (x) {
        x /= 10;
        cntr++;
    }
    return cntr;
}
bool isprime(int x) {
    if (x < 2)return 0;
    for (int i = 2; (ll)i * i <= x; i++) {
        if (x % i == 0)return 0;
    }
    return 1;
}
int primes[N];
void seive(int n) {
    for (int i = 0; i < n; i++)primes[i] = 1;
    primes[0] = primes[1] = 0;
    for (int i = 2; i < N; i++) {
        if (primes[i]) {
            for (int j = i + i; j < n; j += i)primes[j] = 0;
        }
    }
}
int getfirstmultiple(int x) {
    for (int i = 2; i < x; i++) {
        if (x % i == 0)return i;

    }
    return -1;
}
int getprimeFFactorization(int x) {
    for (int i = 2; 1ll * i * i < x; i++) {
        if (x % i == 0) {
            return i;
        }
    }
    return -1;
}
int lucky[] = { 4,7,44,47,74,77,444,447,474,477,744,747,774,777 };











bool rec(int p1, int p2, bool right, string a, string b) {
    if (p2 + 1 == b.size()) {
        return true;
    }
    bool f1 = 0, f2 = 0;

    if (right && p1 + 1 < a.size() && a[p1 + 1] == b[p2 + 1]) {
        f1 = rec(p1 + 1, p2 + 1, 1, a, b);
    }
    if (p1 > 0 && a[p1 - 1] == b[p2 + 1]) {
        f2 = rec(p1 - 1, p2 + 1, 0, a, b);
    }
    if (f1 == 1 || f2 == 1)return 1;
    else return 0;







}

void go() {
    int n, m;
    cin >> n >> m;
    char mm[50 + 2][50 + 2];
    int st1, st2;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> mm[i][j];
            if (mm[i][j] == 'S') {
                st1 = i;
                st2 = j;
            }
        }
    }
    string s;
    cin >> s;
    string maping = "0123";
    int res = 0;
    int temp1 = st1, temp2 = st2;

    do {
        bool flag = false;
        st1 = temp1;
        st2 = temp2;
        for (int i = 0; i < s.size(); i++) {
            int x = maping.find(s[i]);
            if (x == 0)st1--;
            else if (x == 1)st1++;
            else if (x == 2)st2--;
            else if (x == 3)st2++;

            if (st1<0 || st2<0 || st1>n - 1 || st2>m - 1 || mm[st1][st2] == '#') {
                break;
            }
            if (mm[st1][st2] == 'E') {
                flag = true;
                break;
            }

        }
        if (flag) {
           // cout << maping << el;
            res++;

        }


    } while (next_permutation(maping.begin(),maping.end()));

    cout << res;






}





void Beforego() {


}



int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif


    IO
        // online submission
        int cases = 1;
    //cin >> cases;
    Beforego();
    while (cases--) {
        go();
        if (cases)cout << el;
    }

    //cout<<el;

}

  	 	  	 	    			  	 			 				


Comments

Submit
0 Comments
More Questions

1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements